home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.02 Feb 96 / Adding Scripts to Menus / Scripts MenuCode / UScripting.cp < prev    next >
Encoding:
Text File  |  1995-06-15  |  3.1 KB  |  145 lines  |  [TEXT/MMCC]

  1. // ===========================================================================
  2. // UScripting.cp -- support class for scripting
  3. // ===========================================================================
  4. // © 1995 James Kaput, Jeremy Roschelle SimCalc Project
  5.  
  6. #include "UScripting.h"
  7.  
  8. ComponentInstance UScripting::sComponent = nil;
  9.  
  10. void 
  11. UScripting::Initialize()
  12. {
  13.     if (! sComponent) 
  14.         SetComponent(::OpenDefaultComponent(kOSAComponentType,'scpt'));
  15. }
  16.  
  17. OSErr 
  18. UScripting::ParseScriptFile(FSSpec &inFile, Handle &outScript, Handle &outText)
  19. {
  20.     short             fRefNum = -1;
  21.     OSErr            result = noErr;
  22.                 
  23.     Try_ {
  24.         // open resource fork
  25.         fRefNum = ::FSpOpenResFile(&inFile,fsRdPerm);
  26.         ThrowIfResError_();
  27.         
  28.         // get the first script resource in the file
  29.         outScript = ::Get1IndResource('scpt',1); 
  30.         FailNIL_(outScript);
  31.         ::DetachResource(outScript); // detach resource lets us "own" this handle
  32.     
  33.         // the first text resource has the description of the script (if any)
  34.         // we fail quietly if there's no text, since its optional
  35.          outText = ::Get1IndResource('TEXT',1);
  36.          if  (outText) ::DetachResource(outText);
  37.     }
  38.     
  39.     Catch_(catchErr) {
  40.         result = catchErr;
  41.     }
  42.     EndCatch_
  43.     
  44.     if (fRefNum != -1) ::CloseResFile(fRefNum);
  45.     return result;
  46. }
  47.  
  48. OSErr 
  49. UScripting::CreateScriptFile(FSSpec &inFile,Handle inScript, Handle inText)
  50. {
  51.     OSErr    err = noErr;
  52.     Int16        fRefNum = -1;
  53.     Try_ {
  54.         // make the file
  55.         ::FSpCreateResFile(&inFile,'ToyS',kOSAFileType,0);
  56.         ThrowIfResError_();
  57.         
  58.         // open it
  59.         fRefNum = ::FSpOpenResFile(&inFile,fsRdWrPerm);
  60.         ThrowIfResError_();
  61.         
  62.         // copy the script handle and add it to the file
  63.         Handle    data = inScript;
  64.         err = ::HandToHand(&data); 
  65.         ThrowIfOSErr_(err);
  66.         ::AddResource(data,'scpt',128,nil);
  67.         ThrowIfResError_();
  68.         
  69.         // copy the text and add it to the file
  70.         if (inText) {
  71.             data = inText;
  72.             err = ::HandToHand(&data); 
  73.             ThrowIfOSErr_(err);
  74.             ::AddResource(data,'TEXT',1128,nil);
  75.             ThrowIfResError_();
  76.         }
  77.         
  78.         // close (also saves) the resource file
  79.         ::CloseResFile(fRefNum);
  80.         ThrowIfResError_();
  81.     }
  82.     Catch_(catchErr) {
  83.         err = catchErr;
  84.         if (fRefNum != -1) ::CloseResFile(fRefNum);
  85.     }
  86.     EndCatch_
  87.     
  88.     return err;
  89. }
  90.  
  91. OSErr 
  92. UScripting::LoadScript(Handle inScript, OSAID &outScriptID)
  93. {
  94.     Initialize();
  95.     
  96.     AEDesc     scriptDesc;
  97.     scriptDesc.descriptorType = typeOSAGenericStorage;
  98.     scriptDesc.dataHandle = inScript;
  99.     return ::OSALoad(sComponent,&scriptDesc,kOSAModeNull,&outScriptID);
  100. }
  101.  
  102.  
  103. OSErr 
  104. UScripting::LoadScript(AEDesc &inScriptDesc, OSAID &outScriptID)
  105. {
  106.     Initialize();
  107.     return ::OSALoad(sComponent,&inScriptDesc,kOSAModeNull,&outScriptID);
  108. }
  109.  
  110.  
  111. OSErr  
  112. UScripting::ExecuteScript(OSAID inScriptID)
  113. {
  114.     Initialize();
  115.     OSAID            resultID;
  116.     OSErr            err;
  117.     
  118.     err = ::OSAExecute(sComponent,inScriptID,kOSANullScript,kOSAModeNull,&resultID);
  119.     if (err) return err;
  120.     else ::OSADispose(sComponent,resultID);
  121.     
  122.     return noErr;
  123. }
  124.  
  125. OSErr  
  126. UScripting::DisposeScript(OSAID inScriptID)
  127. {
  128.     Initialize();
  129.     return ::OSADispose(sComponent,inScriptID);
  130. }
  131.  
  132.  
  133. URun1Script::URun1Script(OSAID inScriptID) 
  134.     : mScriptID(inScriptID) 
  135. {
  136.     StartRepeating();
  137. }
  138.  
  139. void    
  140. URun1Script::SpendTime(const EventRecord &inMacEvent)
  141. {
  142.     UScripting::ExecuteScript(mScriptID);
  143.     delete this;
  144. }
  145.